package com.example.contacter

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {
    private var adapter: ContactAdapter? = null
    private val handler = Handler()

    private var mManager: LinearLayoutManager? = null
    private var mScroller: TopSmoothScroller? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initData()
        initView()
    }

    private fun initView() {
        indexView.listener = object : IndexView.Listener {
            override fun selected(char: String) {
                showHint(char)
            }
        }
        mManager = LinearLayoutManager(this@MainActivity)
        rv.layoutManager = mManager
        adapter = ContactAdapter(persons)
        rv.adapter = adapter
    }

    fun showHint(char: String) {
        tvCharHint.text = char
        tvCharHint.visibility = View.VISIBLE
        handler.removeCallbacksAndMessages(null)
        handler.postDelayed({ tvCharHint.visibility = View.GONE }, 2500)

        for (i in 0 until persons.size) {
            if (persons[i].pinyin.startsWith(char, true)) {
//                rv.scrollToPosition(i)
                mScroller ?: apply {
                    mScroller = TopSmoothScroller(this@MainActivity)
                }
                mScroller?.targetPosition = i
                mManager?.startSmoothScroll(mScroller)
                break
            }
        }
    }

    /**
     * 初始化数据
     */
    private var persons = arrayListOf(
        Person("张晓飞"), Person("杨光福"), Person("胡继群"), Person("刘畅"),
        Person("钟泽兴"), Person("尹革新"), Person("安传鑫"), Person("张骞壬"),
        Person("温松"), Person("李凤秋"), Person("刘甫"), Person("娄全超"),
        Person("张猛"), Person("王英杰"), Person("李振南"), Person("孙仁政"),
        Person("唐春雷"), Person("牛鹏伟"), Person("姜宇航"), Person("刘挺"),
        Person("张洪瑞"), Person("张建忠"), Person("侯亚帅"), Person("刘帅"),
        Person("乔竞飞"), Person("徐雨健"), Person("吴亮"), Person("王兆霖"),
        Person("阿三"), Person("李博俊"), Person("澄邈"), Person("德泽"),
        Person("海超"), Person("海阳"), Person("海荣"), Person("海逸"),
        Person("海昌"), Person("瀚钰"), Person("瀚文"), Person("涵亮"),
        Person("涵煦"), Person("景平"), Person("涵衍"), Person("浩皛"),
        Person("高朗"), Person("运良"), Person("昆皓"), Person("中震"),
        Person("忆之"), Person("凡灵"), Person("巧蕊"), Person("念瑶"),
        Person("雁山"), Person("芷卉"), Person("绿蝶"), Person("谷槐"),
        Person("夏之"), Person("雅青"), Person("沛凝"), Person("依云")
    )

    private fun initData() {
        //排序
        persons.sortWith(Comparator { lhs, rhs -> lhs.pinyin.compareTo(rhs.pinyin) })
    }
}
